/*
 * log.h
 *
 *  Created on: 2024-01-18
 *      Author: liaofd
 */

#ifndef ___LOG_H__
#define ___LOG_H__

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define xUSE_LOG_LEVEL_COLOR

typedef enum
{
	LOG_PRINTF_DISABLE = 0,
	LOG_INFO,
	LOG_WARNING,
	LOG_ALARML,
	LOG_SHELL
}log_level_t;

#define _shell_echo_gen(format,...) \
do{\
	set_log_level(LOG_SHELL, 3);\
	printf(format, ##__VA_ARGS__);\
	fflush(stdout);\
	set_log_level(LOG_PRINTF_DISABLE, 0);\
}while(0)

#ifndef USE_LOG_LEVEL_COLOR
#define _log_gen(lv, format, ...)\
do{\
	if(!is_log_shell_keep_active())\
	{\
		set_log_level(LOG_INFO, 0);\
		printf("%s [File:%s, Line:%d] " format "\r\n", #lv, __FILE__, __LINE__, ##__VA_ARGS__);\
		fflush(stdout);\
		set_log_level(LOG_PRINTF_DISABLE, 0);\
	}\
}while(0)

#define shell_echo(format,...)		_shell_echo_gen(format, ##__VA_ARGS__)
#define info_log(format,...)		_log_gen(LOG_INFO, format, ##__VA_ARGS__)
#define warning_log(format,...)		_log_gen(LOG_WARNING, format, ##__VA_ARGS__)
#define alarm_log(format,...)\		_log_gen(LOG_ALARM, format, ##__VA_ARGS__)

#else

#define LogColorNone_			"\033[0m"
#define LogColorRed_			"\033[0;31m"
#define LogColorLightRed_		"\033[1;31m"
#define LogColorYellow_			"\033[0;33m"
#define LogColorLightYellow_	"\033[1;33m"
#define LogColorGreen_			"\033[0;32m"
#define LogColorLightGreen_		"\033[1;32m"

#define _log_gen(lv, color, format, ...)\
do{\
	if(!is_log_shell_keep_active())\
	{\
		set_log_level(LOG_INFO, 0);\
		printf(color"%s [File:%s, Line:%d]" format LogColorNone_"\r\n", #lv, __FILE__, __LINE__, ##__VA_ARGS__);\
		fflush(stdout);\
		set_log_level(LOG_PRINTF_DISABLE, 0);\
	}\
}while(0)

#define shell_echo(format,...)		_shell_echo_gen(format, ##__VA_ARGS__)
#define info_log(format,...)		_log_gen(LOG_INFO, LogColorNone_, format, ##__VA_ARGS__)
#define warning_log(format,...)		_log_gen(LOG_WARNING, LogColorYellow_, format, ##__VA_ARGS__)
#define alarm_log(format,...)\		_log_gen(LOG_ALARM, LogColorRed_, format, ##__VA_ARGS__)
#endif

int is_app_direct_call_printf_disable(void);
int is_log_shell_keep_active(void);
void set_log_level(log_level_t lv, int sec);
void log_shell_keep_time_handler(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* APP_SHELL_LOG_H_ */
